package com.tensquare.friend.service;

import com.tensquare.friend.dao.FriendDao;
import com.tensquare.friend.dao.NoFriendDao;
import com.tensquare.friend.pojo.Friend;
import com.tensquare.friend.pojo.NoFriend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

/**
 * Created by WF on 2020/7/29 15:37
 */
@Service
@Transactional
public class FriendService {
    @Autowired
    private FriendDao friendDao;
    @Autowired
    private NoFriendDao noFriendDao;
    //1.添加好友
    public int addFriend(String userId, String friendid) {
        //1.1)查询是否存在好友记录
        int count = friendDao.selectCount(userId,friendid);
        //1.2)判断count，如果大于0，证明有此好友
        if(count > 0) return 0;
        //添加好友
        Friend friend = new Friend(userId,friendid,"0");
        friendDao.save(friend);
        //1.3)查询对方的好友记录
        count = friendDao.selectCount(friendid, userId);
        //1.4)判断count的值
        if(count > 0){      //证明对方加我为好友了
            friendDao.updateLike("1",userId,friendid);
            friendDao.updateLike("1",friendid,userId);
        }
        return 1;
    }
    //2.删除好友
    public void deleteFriend(String id, String friendid) {
        //2.1)从好友表中删除
        friendDao.deleteByUserId(id,friendid);
        //2.2)修改对方的islike的值为0
        friendDao.updateLike("0",friendid,id);
        //2.3)向非好友表添加一条记录
        NoFriend noFriend = new NoFriend(id,friendid);
        noFriendDao.save(noFriend);
    }
}
